<?php
namespace app\admin\controller;

use app\common\model\MessageInform as MessageInformModel;

/**
 * 信息通知控制器
 * Class Messageinform
 * @package app\admin\controller
 */
class Messageinform extends Base
{
    /**
     * [index  消息列表]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new MessageInformModel;
        $map = [];
        $this->getPageAndSize($param);
        $user = $this->user;
        try{
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getList($map,$this->from, $this->size);
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        writelog('查看消息推送列表', 1,$user);
        return admin_json(1, '获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [add  消息添加]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function add()
    {
        $param = input('param.');
        $model = new MessageInformModel;
        $user = $this->user;
        try{
            $param['host'] = config('getui.host');
            $param['appkey'] = config('getui.appkey');
            $param['appid'] = config('getui.appid');
            $param['mastersecret'] = config('getui.mastersecret');
            if($param['send_type'] == 2){
                $param['end_time'] = date('Y-m-d H:i:s',strtotime($param['send_time']) + 6*60);
            }
            $data = posturl('https://getui.fushan8.com/tui.php',$param);

            if($data['code'] == 1){
                $name = '成功';
            }elseif($data['code'] == 2){
                $param['status'] = 2;
                $param['failed_reason'] = $data['result'];
                $name = '失败，错误提示:'.$data['result'];
            }elseif($data['code'] == 3){
                $param['status'] = 2;
                $param['failed_reason'] = '程序错误';
                $name = '失败，程序错误';
            }
            if($param['send_type'] == 2){
                $param['send_time'] = strtotime($param['send_time']);
            }
            $param['taskId'] = $data['data']['taskId'];
            $model->addData($param);
        }catch (\Exception $e) {
            return admin_json(-1,'添加失败'.$e->getMessage());
        }
        writelog('发送消息'.$name,1,$user);
        return admin_json(1, '发送'.$name,$data);
    }
}